/******************************************************************
 ** File Name:

 ** Copyright(c)2005-2011	YLMF. Inc	

 ** Created: ChenYu Xiao  <yunsn0303@gmail.com>

 ** Modified by:

 ** Date:2011-09-29

 ** Description:

 ** Version:0.0.1

 ******************************************************************/


#include <config.h>

#include <gtk/gtk.h>
#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <libintl.h>
#include <locale.h>
#include <sys/types.h>

#include "matcher.h"
#include "common.h"


#define LOCALEDIR "/usr/share/locale/"
#define _(string) gettext(string)
#define N_(string) string


static GtkWidget *window = NULL;

static void
radiobutton_toggled(GtkWidget *, gpointer);

static void
showscreenrb_radiobutton_toggled(GtkWidget *, gpointer);

static void
autologinrb_radiobutton_toggled(GtkWidget *, gpointer);

static GtkWidget *
create_general_page();

static GtkWidget *
create_wallpaper_page();

static GtkWidget *
create_accessibility_page();


static GtkWidget *
create_main_window();


static void
radiobutton_toggled(GtkWidget *widget, gpointer data)
{
        if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
        {
                g_print ("%s\n", data);
        }
}

static void
showscreenrb_radiobutton_toggled(GtkWidget *widget, gpointer data)
{
        ShowLogin *sl = (ShowLogin *)data;
        
        if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
        {
                gtk_widget_set_sensitive(sl->displayeruserlistcb, TRUE);
                gtk_widget_set_sensitive(sl->loginwaittimecb, FALSE);
                gtk_widget_set_sensitive(sl->usercombobox, FALSE);
                gtk_widget_set_sensitive(sl->timespinbutton, FALSE);
                
        }
        
}

static void
autologinrb_radiobutton_toggled(GtkWidget *widget, gpointer data)
{
        ShowAutoLogin *sal = (ShowAutoLogin *)data;
        
        if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
        {
                gtk_widget_set_sensitive(sal->displayeruserlistcb, FALSE);
                gtk_widget_set_sensitive(sal->loginwaittimecb, TRUE);
                gtk_widget_set_sensitive(sal->usercombobox, TRUE);
                gtk_widget_set_sensitive(sal->timespinbutton, TRUE);
                
        }
}




static GtkWidget *
create_general_page()
{
        GtkWidget *vbox;
        GtkWidget *hbox1;
        GtkWidget *hbox2;
        GtkWidget *hbox3;
        GtkWidget *hbox4;
        GtkWidget *hbox5;
        GtkWidget *hbox6;
        
        GtkWidget *showscreenrb;
        GtkWidget *autologinrb;
        
        GtkWidget *displayeruserlistcb;
        GtkWidget *loginwaittimecb;
        GtkWidget *playsoundcb;
        
        GtkWidget *label1;
        GtkWidget *label2;
        GtkWidget *label3;
        GtkWidget *label4;
        
        GtkWidget *usercombobox;
        GtkWidget *sessioncombobox;
        GtkWidget *languagecombobox;
        
        GtkWidget *timespinbutton;
        GtkObject *adjustment;
        
        struct passwd *user;
        struct ShowLogin *sl;
        struct ShowAutoLogin *sal;

        vbox = gtk_vbox_new(FALSE, 6);
        hbox1 = gtk_hbox_new(FALSE, 2);
        hbox2 = gtk_hbox_new(FALSE, 3);
        hbox3 = gtk_hbox_new(FALSE, 3);
        hbox4 = gtk_hbox_new(FALSE, 1);
        hbox5 = gtk_hbox_new(FALSE, 2);
        hbox6 = gtk_hbox_new(FALSE, 2);

        sl = create_show_login();
        sal = create_show_auto_login();

        showscreenrb = gtk_radio_button_new_with_label(NULL,_("Show the screen for chosing who will login"));
        g_signal_connect(G_OBJECT(showscreenrb),"toggled",G_CALLBACK(showscreenrb_radiobutton_toggled),sl);

        autologinrb = gtk_radio_button_new_with_label(gtk_radio_button_get_group(GTK_RADIO_BUTTON(showscreenrb)),_("Login as"));
        g_signal_connect(G_OBJECT(autologinrb),"toggled",G_CALLBACK(autologinrb_radiobutton_toggled),sal);

        displayeruserlistcb = gtk_check_button_new_with_label(_("Display user list at login"));
        loginwaittimecb = gtk_check_button_new_with_label(_("Allow"));
        playsoundcb = gtk_check_button_new_with_label(_("Play sound at login"));
        

        usercombobox = gtk_combo_box_new_text();
        /* get system user */
        while((user = getpwent()) != 0)
        {
                if(kmp_matcher(user->pw_dir, "home"))
                {
                        gtk_combo_box_append_text(GTK_COMBO_BOX(usercombobox), user->pw_name);
                }
        }

        sessioncombobox = gtk_combo_box_new_text();

        languagecombobox = gtk_combo_box_new_text();
        

        adjustment = gtk_adjustment_new(0.0, 0.0, 120.0, 1.0, 0.0, 0.0);
        timespinbutton = gtk_spin_button_new(GTK_ADJUSTMENT(adjustment), 0.01, 2);

        label1 = gtk_label_new (_("automatically"));
        label2 = gtk_label_new (_("seconds for anyone else to login first"));
        label3 = gtk_label_new(_("Select the default session"));
        label4 = gtk_label_new(_("Select the default language"));

        /* init ShowLogin struct */
        set_show_login_usercombobox(sl, usercombobox);
        set_show_login_timespinbutton(sl, timespinbutton);
        set_show_login_loginwaittimecb(sl, loginwaittimecb);
        set_show_login_displayeruserlistcb(sl, displayeruserlistcb);

        /* init ShowAutoLogin struct */
        set_show_auto_login_usercombobox(sal, usercombobox);
        set_show_auto_login_timespinbutton(sal, timespinbutton);
        set_show_auto_login_loginwaittimecb(sal, loginwaittimecb);
        set_show_auto_login_displayeruserlistcb(sal, displayeruserlistcb);

        gtk_box_pack_start(GTK_BOX(hbox1), showscreenrb, FALSE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(hbox1), displayeruserlistcb, FALSE, TRUE, 0);

        gtk_box_pack_start(GTK_BOX(hbox2), autologinrb, FALSE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(hbox2), usercombobox, FALSE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(hbox2), label1, FALSE, TRUE, 0);

        gtk_box_pack_start(GTK_BOX(hbox3), loginwaittimecb, FALSE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(hbox3), timespinbutton, FALSE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(hbox3), label2, FALSE, TRUE, 0);

        gtk_box_pack_start(GTK_BOX(hbox4), playsoundcb, FALSE, TRUE, 0);

        gtk_box_pack_start(GTK_BOX(hbox5), label3, FALSE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(hbox5), sessioncombobox, FALSE, TRUE, 0);

        gtk_box_pack_start(GTK_BOX(hbox6), label4, FALSE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(hbox6), languagecombobox, FALSE, TRUE, 0);

        
        

        gtk_box_pack_start (GTK_BOX(vbox), hbox1, FALSE, TRUE, 0);
        gtk_box_pack_start (GTK_BOX(vbox), hbox2, FALSE, TRUE, 0);
        gtk_box_pack_start (GTK_BOX(vbox), hbox3, FALSE, TRUE, 0);
        gtk_box_pack_start (GTK_BOX(vbox), hbox4, FALSE, TRUE, 0);
        gtk_box_pack_start (GTK_BOX(vbox), hbox5, FALSE, TRUE, 0);
        gtk_box_pack_start (GTK_BOX(vbox), hbox6, FALSE, TRUE, 0);

        return vbox;
}

static GtkWidget *
create_wallpaper_page()
{
        GtkWidget *vbox;
        GtkWidget *hbox1;
        
        GtkWidget *hbuttonBox;

        GtkWidget *image;
        GdkPixbuf *pixbuf;

        GtkWidget *selectbutton;
        

        

        

        vbox = gtk_vbox_new(FALSE, 2);
        hbox1 = gtk_hbox_new(FALSE, 1);

        hbuttonBox =  gtk_hbutton_box_new();

        selectbutton =  gtk_button_new_with_label(_("Select new image"));


        
        gtk_button_box_set_layout(GTK_BUTTON_BOX(hbuttonBox), GTK_BUTTONBOX_DEFAULT_STYLE);

        gtk_box_pack_start(GTK_BOX(hbuttonBox), selectbutton, TRUE, TRUE, 0);

        //        image = gtk_image_new_from_file("/usr/share/backgrounds/ylmfos/nature/ylmfos-2011.jpg");
        pixbuf = gdk_pixbuf_new_from_file("/home/yunsn/pricture/apple.jpg", NULL);
        pixbuf = gdk_pixbuf_scale_simple(pixbuf, 480, 300, GDK_INTERP_BILINEAR);
        
        image = gtk_image_new_from_pixbuf(pixbuf);

        gtk_box_pack_start(GTK_BOX(hbox1), image, TRUE, TRUE, 0);

        gtk_box_pack_start (GTK_BOX(vbox), hbox1, TRUE, TRUE, 0);

        gtk_box_pack_start (GTK_BOX(vbox), hbuttonBox, TRUE, TRUE, 0);

        return vbox;
}

static GtkWidget *
create_accessibility_page()
{
        GtkWidget *vbox;
        GtkWidget *screenkeyboardcb;
        GtkWidget *screenmagnifiercb;
        GtkWidget *screenreadercb;

        vbox = gtk_vbox_new(FALSE, 3);
        
        screenkeyboardcb = gtk_check_button_new_with_label(_("Enable screen keyboard"));
        screenmagnifiercb = gtk_check_button_new_with_label(_("Enable screen magnifier"));
        screenreadercb = gtk_check_button_new_with_label(_("Enable screen reader"));


        gtk_box_pack_start (GTK_BOX(vbox), screenkeyboardcb, FALSE, TRUE, 0);
        gtk_box_pack_start (GTK_BOX(vbox), screenmagnifiercb, FALSE, TRUE, 0);
        gtk_box_pack_start (GTK_BOX(vbox), screenreadercb, FALSE, TRUE, 0);
        



        return vbox;

}



static GtkWidget *
create_main_window()
{
        if (!window)
        {
                GtkWidget *vbox;
                GtkWidget *notebook;
                GtkWidget *generalchild;
                GtkWidget *wallpaperchild;
                GtkWidget *accessibilitychild;
                GtkWidget *label;
                GtkWidget *hbuttonBox;
                GtkWidget *aboutbutton;
                GtkWidget *closebutton;
                
                
                

                /* create window  */
                window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
                gtk_window_set_title (GTK_WINDOW (window), _("Ylmf gdm setup"));
                gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

                g_signal_connect (window, "destroy",
                                  G_CALLBACK (gtk_widget_destroyed), &window);
                gtk_container_set_border_width (GTK_CONTAINER (window), 8);

                vbox = gtk_vbox_new (FALSE, 2);
                gtk_container_add (GTK_CONTAINER (window), vbox);

                notebook = gtk_notebook_new();

                /* create general page */
                generalchild = create_general_page();
                label = gtk_label_new(_("General"));
                gtk_notebook_append_page(GTK_NOTEBOOK(notebook), generalchild, label);

                /* create wallpaperchild page */
                wallpaperchild = create_wallpaper_page();
                label = gtk_label_new(_("Wallpaper"));
                gtk_notebook_append_page(GTK_NOTEBOOK(notebook), wallpaperchild, label);

                /* create accessibility page */
                accessibilitychild = create_accessibility_page();
                label = gtk_label_new(_("Accessibility"));
                gtk_notebook_append_page(GTK_NOTEBOOK(notebook), accessibilitychild, label);
                
                


                hbuttonBox =  gtk_hbutton_box_new();
                
                aboutbutton = gtk_button_new_with_label(_("About"));
                closebutton = gtk_button_new_with_label(_("Close"));




                gtk_button_box_set_layout(GTK_BUTTON_BOX(hbuttonBox), GTK_BUTTONBOX_DEFAULT_STYLE);

                gtk_box_pack_start(GTK_BOX(hbuttonBox), aboutbutton, TRUE, TRUE, 0);
                gtk_box_pack_start(GTK_BOX(hbuttonBox), closebutton, TRUE, TRUE, 0);
                

                gtk_box_pack_start (GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
                gtk_box_pack_start(GTK_BOX(vbox), hbuttonBox, FALSE, TRUE, 0);
                
                

                //gtk_container_add(GTK_CONTAINER(window), notebook);
                
                /* finish & show */
                //  gtk_window_set_default_size (GTK_WINDOW (window), 280, 500);
                g_signal_connect (closebutton, "clicked", G_CALLBACK(gtk_main_quit), NULL);
                g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

        }

        return window;
}





int main(int argc, char *argv[])
{
        GtkWidget *window;


        gtk_init(&argc, &argv);
        
        bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
        bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
        textdomain(GETTEXT_PACKAGE);
        
        window = create_main_window();

        gtk_window_set_resizable(GTK_WINDOW(window), FALSE); 

        gtk_widget_show_all(window);

        gtk_main();

        return 0;

}





